/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "TransactionInfoItem.h"
#include "DiagramView.h"

namespace QSThink
{
	TransactionInfoItem::TransactionInfoItem( DiagramView *view)
		: DiagramViewListener(view), _view(view),_trans(0)
	{
		setFlag(ItemIsSelectable);//ItemIsMovable
		setFlag(ItemSendsGeometryChanges);
		setCacheMode(DeviceCoordinateCache);
		setAcceptHoverEvents(false);

		setZValue(3);
	}

	

	QRectF TransactionInfoItem::boundingRect() const
	{
		int width=_view->maxWidth();

		
		return QRectF(0,0,width, 20);

	}


	QPainterPath TransactionInfoItem::shape() const
	{
		QPainterPath path;
		path.addRect(boundingRect());
		return path;
	}

	void TransactionInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
	{
		painter->setBrush(Qt::darkGray);
		//painter->setPen(QPen(Qt::black,0));
		painter->drawRect(boundingRect());

	
		painter->setPen(Qt::black);


		if(!_trans){
			return;
		}
		Transaction::Type t=_trans->getType();
		switch(t){
			case Transaction::Transaction_1Day:
				{
					Transaction1Day* t=(Transaction1Day*)_trans;
					paint1DayInfo(painter,t);
					break;

				}
				
		}
		
	
	}

	QVariant TransactionInfoItem::itemChange( GraphicsItemChange change, const QVariant &value )
	{
		

		return QGraphicsItem::itemChange(change, value);


	}

	void TransactionInfoItem::mousePressEvent( QGraphicsSceneMouseEvent *evt )
	{
		update();
		QGraphicsItem::mousePressEvent(evt);

	}

	void TransactionInfoItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *evt )
	{
		update();
		QGraphicsItem::mouseReleaseEvent(evt);

	}

	

	TransactionInfoItem::~TransactionInfoItem()
	{
		
	}

	void TransactionInfoItem::OnTransactionChanged( Transaction* trans )
	{
		_trans=trans;

		QGraphicsItem::update();
	}

	void TransactionInfoItem::paint1DayInfo( QPainter * painter, Transaction1Day* t )
	{
		int day=t->day();
		float balancePrice=(float)t->balancePrice()/100.0f;
		float highPrice=(float)t->highPrice()/100.0f;
		float lowPrice=(float)t->lowPrice()/100.0f;
		float openPrice=(float)t->openPrice()/100.0f;
		float closePrice=(float)t->closePrice()/100.0f;
		int volume=t->volume();
		int amount=t->amount();

		painter->setPen(Qt::black);
		painter->setFont(QFont("Arial", 10));


		QString dateStr=Utilities::getDateStrByDay(day);
		int col=1;
		painter->drawText(16,16, "date:");
		col++;
		painter->drawText(22*col,16,dateStr);
		col++;
		painter->drawText(48*col,16, "price:");
		col++;
		painter->drawText(48*col,16,QString::number(balancePrice));
		col++;
		painter->drawText(48*col,16, "high:");
		col++;
		painter->drawText(48*col,16,QString::number(highPrice));
		col++;
		painter->drawText(48*col,16, "low:");
		col++;
		painter->drawText(48*col,16,QString::number(lowPrice));

		col++;
		painter->drawText(48*col,16, "open:");
		col++;
		painter->drawText(48*col,16,QString::number(openPrice));
		col++;
		painter->drawText(48*col,16, "close:");
		col++;
		painter->drawText(48*col,16,QString::number(closePrice));
		col++;
		painter->drawText(48*col,16, "volume:");
		col++;
		painter->drawText(48*col,16,QString::number(volume));

		col++;
		painter->drawText(48*col,16, "amount:");
		col++;
		painter->drawText(48*col,16,QString::number(amount));


	}

	
}
